package activity.addCamera;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import com.hichip.campro.R;
import com.hichip.tools.HiSearchSDK;
import base.HiActivity;
import butterknife.BindView;
import common.HiDataValue;
import common.TitleView;
import main.MainActivity;
import pub.devrel.easypermissions.EasyPermissions;
import utils.HiToast;
import utils.HiTools;
import zbar.TestScanActivity;

/**
 * 选择新设备还是正在使用的设备
 * Created by lt on 2017\12\14 0014.
 */

public class SelectUseOrNewDeviceActivity extends HiActivity implements View.OnClickListener {

    @BindView(R.id.titleview) TitleView titleview;
    @BindView(R.id.tv_new_device) TextView tv_new_device;
    @BindView(R.id.tv_current) TextView tv_current;
    @BindView(R.id.tv_useing_device) TextView tv_useing_device;
    private String mSsid;


    @Override
    protected int setLayoutId() {
        return R.layout.activity_select_add_camera_mode;
    }

    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        initView();
        setListeners();
    }

    private void setListeners() {
        tv_new_device.setOnClickListener(this);
        tv_useing_device.setOnClickListener(this);
    }


    private void initView() {
        titleview.setButton(TitleView.NAVIGATION_BUTTON_LEFT);
        titleview.setTitle(getString(R.string.add_device));
        titleview.setRightTxtBack(R.mipmap.x_bule);
        titleview.setNavigationBarButtonListener(new TitleView.NavigationBarButtonListener() {
            @Override
            public void OnNavigationButtonClick(int which) {
                switch (which) {
                    case TitleView.NAVIGATION_BUTTON_LEFT:
                        SelectUseOrNewDeviceActivity.this.finish();
                        break;
                    case TitleView.NAVIGATION_BUTTON_IV_RIGHT:
                        Intent intent=new Intent(SelectUseOrNewDeviceActivity.this, MainActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent,true);
                        break;
                }

            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_new_device://新设备
                if (HiTools.isWifiConnected(this)) {
                    WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
                    String SSID = wifiManager.getConnectionInfo().getSSID();
                    SSID = SSID.substring(1, SSID.length() - 1);
                    if (SSID.startsWith(HiDataValue.START_WITH_IPCAM)) {
                        showLoadDialog(getString(R.string.searching_device), false, false);
                        startSearch();
                    } else {
                        Intent intent = new Intent(SelectUseOrNewDeviceActivity.this, ConfirmElectriActivity.class);
                        intent.putExtra("type", 1);
                        startActivity(intent);
                    }
                } else {
                    HiToast.showToast(this, getString(R.string.toast_connect_wifi));
                }
                break;
            case R.id.tv_useing_device://正在使用的设备
//              Intent intent = new Intent(SelectUseOrNewDeviceActivity.this, AddCameraActivity.class);
//              startActivity(intent);
                String[] perms = {Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE};
                if(EasyPermissions.hasPermissions(this,perms)){
                    Intent intent=new Intent(SelectUseOrNewDeviceActivity.this, TestScanActivity.class);
                    startActivity(intent);
                }
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (HiTools.isWifiConnected(this)) {
            tv_current.setVisibility(View.VISIBLE);
            WifiManager manager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
            WifiInfo info = manager.getConnectionInfo();
            mSsid = info.getSSID().substring(1, info.getSSID().length() - 1);
            tv_current.setText(getString(R.string.current_network)+ mSsid);
        } else {
            tv_current.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        dissmissLoadDialog();
    }


    private CountDownTimer timer;
    private HiSearchSDK searchSDK;

    private void startSearch() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        int timeLong = 15000;
        searchSDK = new HiSearchSDK(new HiSearchSDK.ISearchResult() {
            @Override
            public void onReceiveSearchResult(HiSearchSDK.HiSearchResult result) {
                String temp = result.uid.substring(0, 4);
                if (!TextUtils.isEmpty(temp)) {
                    Message msg = mHandler.obtainMessage();
                    msg.obj = result;
                    msg.what = HiDataValue.HANDLE_MESSAGE_SCAN_RESULT;
                    mHandler.sendMessage(msg);
                }
            }
        });
        searchSDK.search2();
        timer = new CountDownTimer(timeLong, 1000) {
            @Override
            public void onFinish() {
                dissmissLoadDialog();
                HiToast.showToast(SelectUseOrNewDeviceActivity.this, getString(R.string.not_found_device));
            }

            @Override
            public void onTick(long arg0) {
            }
        }.start();
    }


    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case HiDataValue.HANDLE_MESSAGE_SCAN_RESULT:
                    HiSearchSDK.HiSearchResult searchResult = (HiSearchSDK.HiSearchResult) msg.obj;
                    if (searchResult != null && !TextUtils.isEmpty(searchResult.uid)) {
                        // ap热点 并搜索到了ap机器
                        dismissjuHuaDialog();
                        searchSDK.stop();
                        if (timer != null) {
                            timer.cancel();
                        }
                        Intent intent = new Intent(SelectUseOrNewDeviceActivity.this, ConfirmApDeviceActivity.class);
                        intent.putExtra(HiDataValue.EXTRAS_KEY_UID, searchResult.uid);
                        startActivity(intent);
                    }
                    break;

            }
        }
    };


}











